#include "stm32f10x.h" // Device header
#include "stdint.h"   //声明uint8_t时，要在头文件加上这一行
#include "Delay.h"

void PWM_Init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;
	GPIO_InitTypeDef GPIO_InitStructure; //这个要前放,不然会有警告
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//	 GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);  //冲映射PinA15
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

	GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP ;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	TIM_InternalClockConfig(TIM2);
	TIM_TimeBaseInitstructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitstructure.TIM_CounterMode =TIM_CounterMode_Up ;
	TIM_TimeBaseInitstructure.TIM_Period = 200-1;    //ARR
	TIM_TimeBaseInitstructure.TIM_Prescaler =7200-1;   //PSC
	TIM_TimeBaseInitstructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitstructure);
	 
	  
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;
	TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High ;
	TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable ;         //TIM2,通道2
	TIM_OCInitStructure.TIM_Pulse =500 ;    //CCR的值         //PWM脉宽调制
	TIM_OC2Init(TIM2,&TIM_OCInitStructure);
	
	TIM_OCInitStructure.TIM_Pulse =500;
	TIM_OC1Init (TIM2,&TIM_OCInitStructure );                  //TIM2  通道1
	TIM_OC1PolarityConfig(TIM2,TIM_OCPreload_Enable);        //预转使能
	
	
	TIM_Cmd(TIM2,ENABLE);
 
	      
}

void  PWM_SetCompare1(uint16_t  Compare1)         //动态改变占空比
{
	TIM_SetCompare1(TIM2,Compare1);        //通道1
	
}

void  PWM_SetCompare2(uint16_t  Compare2)         //动态改变占空比
{
	TIM_SetCompare2(TIM2,Compare2);        //通道2
	
}


//void PWM_SetPrescaler(uint16_t Prescaler)
//{
//	TIM_PrescalerConfig(TIM2,Prescaler, TIM_PSCReloadMode_Immediate );
//}

//void Servo_rotate(uint16_t pulse_width)
//{
//	GPIO_SetBits(GPIOA,GPIO_Pin_1);
//	Delay_ms(500);
//	GPIO_ResetBits(GPIOA,GPIO_Pin_1);
//	Delay_ms(20000 -500);
//}
